package MeetingRoom;

import Meeting.MeetingDAO;
import gongju.Communtil;
import gongju.JDBC;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MeetingRoomDAO {

    private int id;
    private String name;
    private String state;
    private String user;
    public static final Object[] columnNames = {"ID","室名","维修及处理","是否使用"};

    public MeetingRoomDAO() {}

    public MeetingRoomDAO(int id,String name,String state,String user) {
        this.id = id;
        this.name = name;
        this.state = state;
        this.user = user;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }

    //多条件查询（模糊查询）查询会议室
    public static Object[][] searvhMulti(int[] cols,String[] texts){
        String sql = "select * from meetingroom where 1=1";
        List param = new ArrayList();
        if(cols!=null && cols.length>0){
            for(int i=0;i<cols.length;i++){
                if(Communtil.isEmpty(texts[i])){
                    continue;
                }
                switch(cols[i]){
                    case 1:
                        sql += " and name like concat('%',?,'%')";
                        break;
                        case 3:
                            sql += " and user like concat('%',?,'%')";
                            break;
                }
                param.add(texts[i]);
            }
        }
        return Communtil.toArray(JDBC.queryList(sql,param,4));
    }

    //会议室ID自增
    public static boolean add(Object[] obj){
        String sql = "insert into meetingroom(name,state,user) values(?,?,?)";
        List param = new ArrayList();
        for(int i=0;i<obj.length;i++){
            param.add(obj[i]);
        }
        return JDBC.executeUpdate(sql,param) > 0;
    }

    //在给定的数据集合中查找与指定ID相匹配的对象，并返回该对象。
    public static Object[] findById(Object id){
        String sql = "select * from meetingroom where id=?";
        List param = new ArrayList();
        param.add(id);
        return JDBC.queryObject(sql,param,1);
    }


    public static void  updata(Object id,Object[] obj){
        String sql = "update meetingroom set name=?,state=?,user=? where id=?";
        List param = new ArrayList();
        for(int i=0;i<obj.length;i++){
            param.add(obj[i]);
        }
        param.add(id);
        JDBC.executeUpdate(sql,param);
    }

    public static void remove(Object id){
        String sql = "delete from meetingroom where id=?";
        List param = new ArrayList();
        param.add(id);
        JDBC.executeUpdate(sql,param);
    }

    public static List<MeetingRoomDAO> getAll(){
        Connection conn = JDBC.getConnection();
        String sql = "select * from meetingroom";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<MeetingRoomDAO> list = new ArrayList<>();
            while (rs.next()){
                MeetingRoomDAO meetingroom = new MeetingRoomDAO();
                meetingroom.setId(rs.getInt("id"));
                meetingroom.setName(rs.getString("name"));
                meetingroom.setState(rs.getString("state"));
                meetingroom.setUser(rs.getString("user"));
                list.add(meetingroom);
            }
            JDBC.closeAll(conn,ps, rs);
            exportMeetingDAOTOTxt(list,"D:\\txt\\文件.txt");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void exportMeetingDAOTOTxt(List<MeetingRoomDAO> meetingroomlist, String filePath) {
        try(BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))){
            for(MeetingRoomDAO meetingroom : meetingroomlist){
                writer.write(meetingroom.getId()+","+meetingroom.getName()+","+meetingroom.getState()+","
                        +meetingroom.getUser());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
